<?php
class UserAction extends Action{

	function init(){
		UserModel::isLogin(true);
		$usertype=intval(getInf('USER_TYPE'));
		$this->assign('usertype', $usertype);
		$user=new MyModel();
		$us=$user->all;
		$this->assign('u', $us);
	}
	
	function cmdIndex(){
		$userid=getInf("USER_ID");
		$user=new UserModel();
		$userid=verify_id($userid);
		$u=$user->getInfo($userid);
		$this->assign('u', $u);
		$this->assign("title",'用户中心');
		$this->display("user/index.htm");
	}

	function cmdEdituser(){
		$userid=getInf('USER_ID')?getInf('USER_ID'):0;
		$user=new UserModel();
		$result=$user->getInfo($userid);

		$this->assign('u', $result);
		$this->assign('title', L("MEMBERINFOEDIT"));
		$this->display("user/edituser.htm");
	}

	function cmdEditusersubmit(){
		$userid=getInf('USER_ID')?getInf('USER_ID'):0;
		$username=isset($_POST['username'])?trim($_POST['username']):null;
		$usernick=isset($_POST['usernick'])?trim($_POST['usernick']):null;
		$password=isset($_POST['password'])?trim($_POST['password']):null;
		$password2=isset($_POST['password2'])?trim($_POST['password2']):null;
		$userhead=isset($_POST['userhead'])?trim($_POST['userhead']):null;
		$phone=isset($_POST['phone'])?trim($_POST['phone']):null;
		$sex=isset($_POST['sex'])?trim($_POST['sex']):null;
		$usermobile=isset($_POST['usermobile'])?trim($_POST['usermobile']):null;

		$msg=null;
		$user=new UserModel();
		$userinfo=$user->getInfo($userid);
		//if ($username=='' || strlen($username)<3)$msg.='用户名不能为空或小于3字符⊙﹏⊙b汗<br />';
		if ($usernick=='' || strlen($usernick)<2)$msg.='昵称不能为空或小于2字符\(^o^)/~<br />';
		if (md5($password)!=$userinfo['user_pass'])$msg.='旧密码输入不正确\O(∩_∩)O~<br />';
		if ($password2=='' || strlen($password2)<6)$msg.='新密码不能为空或小于6字符\O(∩_∩)O~<br />';
		if (strlen($phone)!=11 || !is_numeric($phone))$msg.='手机号码必须为11位数字<br />';
		if (empty($msg)){
			
			@$result=$user->Update($username, $usernick,md5($password2),$phone,$sex,$usermobile,$userhead,$userid);
			if ($result=='OK'){
				$this->Message(L("DATA_MODIFIED"),L("SUCCESS_MODIFIED"),PATH.'/index.php/user',3);
			}else{
				$this->Message($result);
			}
		}else{
			$this->Message($msg,L("SUCCESS_MODIFIED"),PATH.'/index.php/user/Edituser',3);
		}
	}
	
	function cmdSmslist(){
		$userid=getInf('USER_ID')?getInf('USER_ID'):0;
		$userid=verify_id($userid);
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		
		$mao=new MaoModel();
		$list=$mao->getMaoList($userid,$page,$pagesize);
		$this->assign('title', '指令列表');
		$this->assign('list', $list['data']); 
		$this->assign('pagesize',$pagesize);
		$this->assign('page',$page);
		$this->assign('count', $list['count']);
		$this->assign('pagecount', $list['pagecount']);
		//var_dump($list);
		$this->display('user/maolist.htm');
	}
	
	function cmdEditsms(){
		$maoid=isset($_GET['id'])?$_GET['id']:0;
		$mao=new MaoModel();
		$m=$mao->getMao($maoid);
		$this->assign("m", $m);
		$this->assign('title', "配置指令");
		$this->display('user/maoedit.htm');
	}
	
	function cmdEditsmssubmit(){
		$command_id=isset($_POST['command_id'])?trim($_POST['command_id']):null;
		$userid=getInf('USER_ID')?getInf('USER_ID'):0;
		
		$msg=null;
		$maourl=isset($_POST['maourl'])?trim($_POST['maourl']):null;
		$maomsg=isset($_POST['maomsg'])?trim($_POST['maomsg']):null;
		$maokey=isset($_POST['maokey'])?trim($_POST['maokey']):null;
		
		if (strlen($maourl)<7 || strlen($maourl)>255 || substr($maourl,0,7)!='http://')$msg.="注册接口不正确(接口需要包含http://)<br />";
		if (strlen($maomsg)<2 || strlen($maomsg)>255 || strlen($maomsg)>20)$msg.="指令必须在2至20字符之间,不支持中文与符号<br />";
		if (strlen($maokey)<2 || strlen($maokey)>255)$msg.="通信密令必须在2至255字符之间,不支持中文与符号<br />";
		
		if ($command_id){
			if(empty($msg)){
				$command_id=verify_id($command_id);
				$mao=new MaoModel();
				$result=$mao->updateMao($command_id, $maourl, $maomsg,$maokey);
				if ($result=='OK'){
					$this->Message("修改成功！",'修改成功',url(array($GLOBALS['METHOD']=>'Index')),3);
				}else{
					$this->Message($result,'修改失败',url(array($GLOBALS['METHOD']=>'Smslist')),3);
				}
			}else{
				$this->Message($msg,'修改失败',url(array($GLOBALS['METHOD']=>'Smslist')),3);
			}
		}/* elseif ($userid){
			if(empty($msg)){
				$mao=new MaoModel();
				$result=$mao->addMao($userid, $maourl, $maomsg,$maokey,0);
				if ($result=='OK'){
					$this->Message("新增成功！",'新增成功',str_replace('Editsmssubmit', 'Index', getUrl(false)),3);
				}else{
					$this->Message($result,'新增失败',str_replace('Editsmssubmit', 'Smslist', getUrl(false)),3);
				}
			}else{
				$this->Message($msg,'新增失败',str_replace('Editsmssubmit', 'Smslist', getUrl(false)),3);
			}
		} */else{
			$this->Message("参数不正确",'错误提示',url(array($GLOBALS['METHOD']=>'Smslist')),3);
		}
	}
	
	function cmdAddsms(){
		$m=array();
		$this->assign("m", $m);
		$this->assign('title', "购买指令");
		$this->display('user/maoadd.htm');
	}

    function cmdbuyMao(){
        $userid=getInf('USER_ID')?getInf('USER_ID'):0;
        //$command_id=verify_id($command_id);
        $msg=null;
        $maourl=isset($_POST['maourl'])?trim($_POST['maourl']):null;
        $maomsg=isset($_POST['maomsg'])?trim($_POST['maomsg']):null;
        $maokey=isset($_POST['maokey'])?trim($_POST['maokey']):null;
        $deadline=isset($_POST['deadline'])?trim($_POST['deadline']):null;
        $deadline = verify_id($deadline);

        if (strlen($maourl)<7 || strlen($maourl)>255 || substr($maourl,0,7)!='http://')$msg.="注册接口不正确(接口需要包含http://)<br />";
        if (strlen($maomsg)<2 || strlen($maomsg)>255 || strlen($maomsg)>20)$msg.="指令必须在2至20字符之间,不支持中文与符号<br />";
        if (strlen($maokey)<2 || strlen($maokey)>255)$msg.="通信密令必须在2至255字符之间,不支持中文与符号<br />";

        if(empty($msg)){
            $user = new UserModel();
            $u = $user->getInfo($userid);
            $total_money = intval($deadline) * SiteConfig::get('month_money');
            if($u['user_money'] < $total_money){
                $this->Message('抱歉！你的余额不足,还差'.(SiteConfig::get('month_money') * intval($deadline) - $u['user_money']).L('MONEY_NAME'),'购买失败',url(array($GLOBALS['METHOD']=>'Smslist')),3);
            }
            $mao=new MaoModel();
            $result=$mao->addMao($userid, $maourl, $maomsg, $maokey, $deadline);
            if ($result=='OK'){
                $this->Message("购买成功！扣除".(SiteConfig::get('month_money') * intval($deadline)).L('MONEY_NAME'),'购买成功',url(array($GLOBALS['METHOD']=>'Smslist')),3);
            }else{
                $this->Message($result,'购买失败',url(array($GLOBALS['METHOD']=>'Smslist')),3);
            }
        }else{
            $this->Message($msg,'购买失败',url(array($GLOBALS['METHOD']=>'Smslist')),3);
        }
    }

	/**
	 * 删除一个短信指令
	 */
	function cmdDelsms(){
		$command_id=isset($_GET['id'])?trim($_GET['id']):null;
		$command_id=verify_id($command_id);
		$mao=new MaoModel();
		if (isset($_GET['del'])){ 
			$result=$mao->delMao($command_id);
			$this->Message($result,'删除成功',url(array($GLOBALS['METHOD']=>'Smslist')),3);
		}else{
			$m=$mao->getMao($command_id);
			$this->assign('title', '删除指令');
			$this->assign('m', $m);
			$this->display('user/maodel.htm');
		}
	}
	
	function cmdSmslog(){
		$command_id=isset($_GET['id'])?$_GET['id']:null;
		$command_id=verify_id($command_id);
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		$log=new LogModel();
		$l=$log->getList(null,$command_id,null,$page,$pagesize);
		$this->assign('title', '查看日志');
		$this->assign('id', $command_id);
		$this->assign('l', $l['data']);
		$this->assign('pagesize',$pagesize);
		$this->assign('page',$page);
		$this->assign('count', $l['count']);
		$this->assign('pagecount', $l['pagecount']);
		$this->display('user/loglist.htm');
	}
	
	function cmdSmserrorlog(){
		$command_id=isset($_GET['id'])?$_GET['id']:null;
		$command_id=verify_id($command_id);
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		
		$log=new LogModel();
		$l=$log->getList(null,null,null,$page,$pagesize);
		$this->assign('title', '查看所有错误日志');
		$this->assign('id', $command_id);
		$this->assign('l', $l['data']);
		$this->assign('pagesize',$pagesize);
		$this->assign('page',$page);
		$this->assign('count', $l['count']);
		$this->assign('pagecount', $l['pagecount']);
		$this->display('user/logerrorlist.htm');
	}
	/**
	 * 日志调用
	 */
	function cmdgetLog(){
		$userid=getInf('USER_ID')?getInf('USER_ID'):0;
		$this->assign('userid', $userid);
		$this->assign('title', '日志调用');
		$this->display('user/getlog.htm');
	}
	
	function cmdSmsRenewal(){
		$command_id=isset($_REQUEST['id'])?$_REQUEST['id']:null;
		$command_id=verify_id($command_id);
		
		$mao = new MaoModel();
		$m = $mao->getMao($command_id);
		if($m == null)$this->Message('续费的指令不存在!','操作失败',url(array($GLOBALS['METHOD']=>'Smslist')),3);
		
		$this->assign('m', $m);
		$this->assign('title', '指令续费');
		$this->display('user/sms_renewal.htm');
	}
	/**
	 * 续费确认
	 */
	function cmdRenewalConfirm(){
		$command_id=isset($_POST['command_id'])?trim($_POST['command_id']):null;
		$deadline=isset($_POST['deadline'])?trim($_POST['deadline']):null;
		$act = isset($_GET['act'])?$_GET['act']:null;
		
		$command_id = verify_id($command_id);
		$deadline = verify_id($deadline);

        $userid=getInf("USER_ID");
        $user=new UserModel();
        $userid=verify_id($userid);
        $u=$user->getInfo($userid);

		$month_money = SiteConfig::get('month_money');
		
		if(empty($act)){
            $this->assign('total_money', $month_money * intval($deadline));
            $this->assign('u', $u);
            $this->assign('title','续费确认');
            $this->assign('deadline',$deadline);
            $this->assign('command_id', $command_id);
            $this->display('user/renewal_confrim.htm');
        }else{
            if($u['user_money'] >= $month_money * intval($deadline)){
                $mao = new MaoModel();
                $rt = $mao->Renewal($command_id, $deadline);
                if($rt){
                    $this->Message('恭喜续费成功！扣除'.$month_money.L('MONEY_NAME'),'续费成功',
                    url(array($GLOBALS['METHOD']=>'Smslist')),3);
                }else{
                    $this->Message('抱歉！续费失败,未知原因,建议联系管理员',
                        '续费失败',url(array($GLOBALS['METHOD']=>'SmsRenewal')),3);
                }
            }else{
                $this->Message('抱歉！你的余额不足,还差'.($month_money * intval($deadline) - $u['user_money']).L('MONEY_NAME'),
                '续费失败',url(array($GLOBALS['METHOD']=>'SmsRenewal')),3);
            }

        }
		
	}
 
}